package com.cloudxingkong.dianyinke.Student.Fragment;


import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Fragment.BaseFragment;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.DpjAdapter;
import com.cloudxingkong.dianyinke.Student.bean.QbfkEntity;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * 待评价fragment
 */
public class DPJ_Fragment extends BaseFragment {

    private List<QbfkEntity.ListBean> list;
    private ListView listView;
    private DpjAdapter adapter;
    private SharedPreferences sharedPreferences;
    private ProgressDialog progressDialog;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_qbfk_, null);

        init();
        loadData();


        return  view;
    }

    private void loadData() {
        progressDialog = ProgressDialog.show(getActivity(),null,"正在加载...",true,false);
        String sessionid = CookieHelper.getSessionID(sharedPreferences);
        String upLoadUrl = ThisUri.SERVER_YUMING+ StudentUri.DEMANDORDER;
        RequestParams params = new RequestParams(upLoadUrl);
        params.addHeader("cookie", sessionid);
        params.addBodyParameter("status","4");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("test", "待评价" + result);
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = result;
                handler.sendMessage(message);
                 progressDialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                String state = (String) msg.obj;
                if (state!=null){
                    Gson gson = new Gson();
                    QbfkEntity entity = gson.fromJson(state,QbfkEntity.class);
                    list.addAll(entity.getList());
                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }else {
                    Toast.makeText(getContext(),"您还没又兑换商品，赶快去兑换吧！！！",Toast.LENGTH_SHORT).show();
                }
            }
        }
    };
    private void init() {
        listView = (ListView) view.findViewById(R.id.lv_qbfk);
        list = new ArrayList<>();
        adapter = new DpjAdapter(list,getActivity());
        sharedPreferences=getActivity().getSharedPreferences("data_dyk",0);
    }

}
